//
//  ViewController.m
//  UI05-彩虹灯
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CutornButton.h"
#import "ColourView.h"
@interface ViewController ()
@property (retain, nonatomic)NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createColourView];
    [self createCustomButton];
    
    [self.timer setFireDate:[NSDate distantFuture]];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)createColourView {
    NSArray *color = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor blackColor], [UIColor purpleColor], nil];
    
    
    for (int i = 0; i < 7; i++) {
        ColourView *view = [ColourView colourViewWithFrame:CGRectMake(10 + 10 * i, 50 + 50 * i, self.view.frame.size.width - (10 + 10 * i) * 2 , self.view.frame.size.width - (10 + 10 * i) * 2) color:color[i]];
        [self.view addSubview:view];
    }
    
}
- (void)createCustomButton {
    CutornButton *start = [CutornButton customButtonWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 2 + 50, 60, 30) title:@"开始"];
    
    CutornButton *stop = [CutornButton customButtonWithFrame:CGRectMake(self.view.frame.size.width / 2 + 30, self.view.frame.size.height / 2 + 50, 60, 30) title:@"停止"];
    [start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:start];
    [self.view addSubview:stop];
}

- (void)start {
    if (self.timer == nil) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeViewColor:) userInfo:nil repeats:YES];
    }
    
}
- (void)stop {
    if (self.timer != nil) {
        [self.timer invalidate];
        NSLog(@"%ld", [self.timer retainCount]);
        self.timer = nil;
    }
    
}

- (void)changeViewColor:(UIButton *)button {
    NSArray <UIView *>*arr = self.view.subviews;
    UIColor *temp = arr[0].backgroundColor;
    for (int i = 0; i < 6; i++) {
        arr[i].backgroundColor = arr[i + 1].backgroundColor;
    }
    arr[6].backgroundColor = temp;
}

@end
